﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections; 

using YT.Commons;
using YT.DB;
 
namespace YT.XH.MF
{
    public partial class frmmHospitalInfoMtn : Form
    {

        private string mstrTableName = "mHospital";
        private DBAccess mdbcon = new DBAccess(false, false);
        private ArrayList mArrayControl = new ArrayList();
        private string mstrRecid = string.Empty;
        public frmmHospitalInfoMtn()
        {
            InitializeComponent();
        }

        private void frmmHospitalInfoMtn_FormClosed(object sender, FormClosedEventArgs e)
        {
            SysVar.marrFormList.Remove(this.Name);
            mdbcon.CloseCon();
        }


        private bool Update()
        {
            bool blnIsNew = false;
            string strFileds = string.Empty;
            string strSQL = string.Empty;
            strSQL = "select * from " + mstrTableName +" where recid='"+ mstrRecid +"'"  ;
            DataRow drRow= mdbcon.GetDataFirstRow(strSQL);
            if (drRow == null)
            {
                drRow = mdbcon.GetDataTable(strSQL).NewRow();
                blnIsNew = true;
            }
            strFileds = DataFunctions.GetUpdateSQL(mstrTableName, "");
            foreach (Control conTxtBox in this.grpTextBox.Controls)
            {
                drRow["RecID"] = CvtFunc.ToInt(mstrRecid);   
                foreach (string strField in strFileds.Split(','))
                {
                    if (conTxtBox.GetType().Name == "TextBox" && GetFieldName( conTxtBox.Name).ToUpper()  == strField.ToUpper()  )
                    {
                        drRow[strField] = conTxtBox.Text;  
                    }
                }                
            }
            if ( blnIsNew ==true )
                DataFunctions.UpdateTable(mdbcon, strFileds, mstrTableName, drRow, FormStatus.AddNew);             
            else 
                DataFunctions.UpdateTable(mdbcon, strFileds, mstrTableName, drRow, FormStatus.Update);

            mstrRecid = CvtFunc.ToString(drRow["RecID"]);
            return true;
        }


        private void initControl()
        {
            string strSQL = @"    select c.name as Type,a.name,a.length from [dbo].syscolumns  a  
                            inner join  [dbo].sysobjects b on a.id=b.id 
                            inner join SYSTYPES C  on a.xtype=c.xtype
                            inner join syscomments D on a.cdefault=d.id
                            and a.id = object_id( '" + this.mstrTableName + "') and (c.name='varchar' or c.name='char')";

            DataTable dtTable = mdbcon.GetDataTable(strSQL);

            foreach (DataRow drRow in dtTable.Rows)
            {
                foreach (Control con in mArrayControl)
                {
                    if (con.GetType().Name == "TextBox")
                    {
                        if (CvtFunc.ToString(drRow["Name"]).Trim().ToUpper() == GetFieldName(con.Name).ToUpper().Trim())
                            ((TextBox)con).MaxLength = CvtFunc.ToInt(drRow["Length"]);
                    }
                }
            }

        }
        private void FillTextBox()
        {
            string strSQL = string.Empty;
            string[] strFieldName;
            strFieldName = DataFunctions.GetUpdateSQL(mstrTableName, "").Split(new char[]{','}) ;

            strSQL = "select top 1 * from " + mstrTableName + " where IsCurHospital=1 ";
            DataRow drRow = mdbcon.GetDataFirstRow(strSQL);
            if (drRow != null)
            {
                foreach (string strField in strFieldName)
                {
                    foreach (Control Con in mArrayControl)
                    {                    
                        if ( GetFieldName(Con.Name).ToUpper()== strField.ToUpper() )
                        {
                            Con.Text = CvtFunc.ToString(drRow[strField]);  
                        }
                    }
                }
                mstrRecid =CvtFunc.ToString ( drRow["RecID"]);
            }   
        }

        private string GetFieldName( string con )
        {
            if (con.Length > 3)
            {
                return con.Substring(3, con.Length - 3);
            }
            return "";
        }

        private void SelectNextText(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
                SelectNextControl((Control)sender, true, true, true, false);
        }

        private void frmmHospitalInfoMtn_Load(object sender, EventArgs e)
        {
            this.Text = SysVar.CurrentMenuInfo.strMenuName;
            mArrayControl.Clear();
            foreach (Control con in this.grpTextBox.Controls)
            {
                mArrayControl.Add(con);
                con.KeyDown += new System.Windows.Forms.KeyEventHandler(this.SelectNextText); 
            }
            initControl();
            FillTextBox();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if  (Update()) this.Close() ;
        }
    }
}
